package com.example.ssmDemo.business.student.controller;

/**
 * 模块名称：
 * 模块类型：
 * 编码人：刘彬
 * 生成时间：2023/2/21
 * 联系电话：
 * */
import com.example.ssmDemo.core.dto.MyPager;
import com.example.ssmDemo.core.dto.MyResult;
import com.example.ssmDemo.mybatis.domain.student.StudentDO;
import com.example.ssmDemo.business.student.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 学生基础信息采集模块-控制器层
 * 2022/6/1
 */
@Controller
@RequestMapping("/student")
@Slf4j //日志注解 自动生成一个log对象
public class StudentController {

    // 注入业务层实例
    @Resource(name = "studentServiceImpl")
    private StudentService studentService;

    @RequestMapping(value = "/getStudentByID",method = RequestMethod.GET)
    @ResponseBody
    public MyResult getStudentByID(String id){
//        log.debug("学生信息管理:获取学生信息通过学生id [param:id"+id+"]");
        MyResult result = new MyResult();
        //调用业务层
        StudentDO studentDO = studentService.getStudentByID(id);
        if(studentDO==null){
            result.setCode(500);
            result.setMsg("没有查询到任何学生数据！");
        }else{
            result.setMsg("学生信息查询成功！");
            result.setData(studentDO);
        }
        return result;
    }

    /**
     * 分页信息
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/getStudentPage",method = RequestMethod.GET)
    @ResponseBody
    public MyResult getStudentPage(Integer page,Integer size){
        MyPager myPager=new MyPager();
        myPager.setPage(page);
        myPager.setSize(size);
//        log.debug("学生信息管理:获取学生信息");
        MyResult result = new MyResult();
        //调用业务层
        MyPager<StudentDO> studentDO = studentService.getStudentPage(myPager);
        result.setData(studentDO);
        System.out.println(result);
        result.setMsg("查询成功");
        return result;
    }

    /**
     * 删除模块
     * @param ids //前端对象数组
     * @return
     */
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.POST)
    @ResponseBody
    public MyResult deleteStudent(@RequestBody String[] ids){
        log.debug(ids.toString());
        System.out.println("我的id"+ids.toString());
        MyResult result = new MyResult();
       int i=0;
        for (String id : ids) {
           i=i+studentService.deleteStudent(id);
       }
       if (i!=ids.length-1){
           result.setMsg("删除失败");
           result.setCode(9999);
       return result;
       }
       result.setMsg("删除成功");
       result.setCode(0000);
       return result;
    }

    /**
     * 修改模块
     * @param studentDO
     * @return
     */
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    @ResponseBody
    public MyResult updateStudent(@RequestBody StudentDO studentDO){
        MyResult result = new MyResult();
        int i= studentService.updateStudent(studentDO);
        if (i!=1){
            result.setCode(9999);
            result.setMsg("修改失败");
            return result;
        }else {
            result.setCode(0000);
            result.setMsg("修改成功");
        }
        return result;
    }

    /**
     * 添加模块
     * @param studentDO
     * @return
     */
    @RequestMapping(value = "/insertStudent",method = RequestMethod.POST)
    @ResponseBody
    public MyResult insertStudent(@RequestBody StudentDO studentDO){
        MyResult result = new MyResult();
        int i= studentService.insertStudent(studentDO);
        if (i!=1){
            result.setCode(9999);
            result.setMsg("添加失败");
            return result;
        }else {
            result.setCode(0000);
            result.setMsg("添加成功");
        }
        return result;
    }
    @RequestMapping(value = "/toMain",method = RequestMethod.GET)
    public String toMainPage(){
        return "/student/main";
    }

}
